// PITAEthnicity

log using PITAEthnicity.log, replace

use "C:\Users\sbstjp\OneDrive - Cardiff University\Transgender IAT.public.2023.dta" // Project Implicit Demo Website Datasets, Transgender IAT, Last Updated: 2025-01-13 10:29 PM. Date accessed: March 09, 2025. SPSS version of the dataset downloaded and then, without alterations, converted to a Stata file. 

// Create the scale

*Standardize items in the scale from 1-2 - this puts the scale on the same scale as others in the book and avoids 0, for reasons outlined in next step
foreach var in transphobia1 transphobia2 transphobia3 transphobia4 transphobia5 transphobia6 transphobia7 transphobia8 transphobia9 {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values
foreach var in stransphobia1 stransphobia2 stransphobia3 stransphobia4 stransphobia5 stransphobia6 stransphobia7 stransphobia8 stransphobia9 {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreTP = 0
gen count_nonzeroTP = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in stransphobia1 stransphobia2 stransphobia3 stransphobia4 stransphobia5 stransphobia6 stransphobia7 stransphobia8 stransphobia9 {
    replace total_scoreTP = total_scoreTP + `var'
    replace count_nonzeroTP = count_nonzeroTP + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen TPScale = .
replace TPScale = total_scoreTP / count_nonzeroTP if count_nonzeroTP > 0

* Reverse so social justice values are coded high
qui sum TPScale
gen NonTransphobia = r(max) + 1 - TPScale

// Demographics
*This is a convenience sample and under 18s have completed it. But we're studying voters so drop under 18s.
drop if birthyear>2005 
gen age = 2023 - birthyear 
*Drop as probably misreported age
drop if age>100
 
* Rename variables and create dummies
rename birthSex FemaleGender

gen Graduate=.
replace Graduate=0 if inrange(edu_14, 1, 6)
replace Graduate=1 if inrange(edu_14, 7, 14)

gen White=.
replace White=1 if raceomb_002==6
replace White=0 if inrange(raceomb_002, 1, 5)
replace White=0 if inlist(raceomb_002, 7, 8)

gen Black=.
replace Black=1 if raceomb_002==5
replace Black=0 if inrange(raceomb_002, 1, 4)
replace Black=0 if inrange(raceomb_002, 6, 8)

gen EastAsian=.
replace EastAsian=1 if raceomb_002==2
replace EastAsian=0 if raceomb_002==1
replace EastAsian=0 if inrange(raceomb_002, 3, 8)

gen SouthAsian=.
replace SouthAsian=1 if raceomb_002==3
replace SouthAsian=0 if inlist(raceomb_002, 1, 2)
replace SouthAsian=0 if inrange(raceomb_002, 4, 8)

// Standardize
egen Age = std(age) 

// Create a weight

gen weight = 1

* Code age into categories - the other variables are already in such categories
recode age (min/24=1 "0-24") (25/34=2 "25-34") (35/44=3 "35-44") (45/54=4 "45-54") (55/max=5 "55+"), generate(age_group)

* The below are based on census data, except for ideology, which is based on data from the ANES24 pilot study
gen sextot=.
replace sextot = 0.49 if FemaleGender == 1
replace sextot = 0.51 if FemaleGender == 2

gen agetot=.
replace agetot = 0.12 if age_group == 1 // 18-24
replace agetot = 0.17 if age_group == 2 // 25-34
replace agetot = 0.17 if age_group == 3 // 35-44
replace agetot = 0.16 if age_group == 4 // 45-54
replace agetot = 0.38 if age_group == 5 // 55+

gen idtot=.
replace idtot = 0.08 if politicalid_7 == 1 // Strongly conservative
replace idtot = 0.17 if politicalid_7 == 2 // Moderately conservative
replace idtot = 0.08 if politicalid_7 == 3 // Slightly conservative
replace idtot = 0.33 if politicalid_7 == 4 // Neutral
replace idtot = 0.08 if politicalid_7 == 5 // Slightly liberal
replace idtot = 0.18 if politicalid_7 == 6 // Moderately liberal
replace idtot = 0.08 if politicalid_7 == 7 // Strongly liberal

gen edcats=.
replace edcats=1 if inrange(edu_14, 1, 4) // Highschoolandbelow
replace edcats=2 if inrange(edu_14, 5, 6) // Somecollege
replace edcats=3 if edu_14==7 // Bachelors
replace edcats=4 if inrange(edu_14, 8, 14) // Graduate

gen edtot=.
replace edtot = 0.3875 if edcats == 1
replace edtot = 0.2644 if edcats == 2
replace edtot = 0.2208 if edcats == 3
replace edtot = 0.1273 if edcats == 4

gen ethcats=.
replace ethcats=1 if raceomb_002==1 //AmericanIndian/AlaskaNative
replace ethcats=2 if inrange(raceomb_002, 2, 3) //Asian
replace ethcats=3 if raceomb_002==4 //NativeHawaiian
replace ethcats=4 if raceomb_002==5 //Black
replace ethcats=5 if raceomb_002==6 //White
replace ethcats=6 if raceomb_002==7 //Other
replace ethcats=7 if raceomb_002==8 //Multiracial

gen ethtot=.
replace ethtot = 0.01 if ethcats == 1
replace ethtot = 0.0599 if ethcats == 2
replace ethtot = 0.002 if ethcats == 3
replace ethtot = 0.1213 if ethcats == 4
replace ethtot = 0.6051 if ethcats == 5
replace ethtot = 0.0742 if ethcats == 6
replace ethtot = 0.1276 if ethcats == 7

survwgt rake weight , by(FemaleGender age_group politicalid_7 ethcats edcats) totvars(sextot agetot idtot ethtot edtot) generate(rakedweight)

// Regressions and correlations
regress NonTransphobia Age FemaleGender Graduate White [pweight=rakedweight], robust 
eststo
regress NonTransphobia Age FemaleGender Graduate Black [pweight=rakedweight], robust 
eststo
regress NonTransphobia Age FemaleGender Graduate EastAsian [pweight=rakedweight], robust 
eststo
regress NonTransphobia Age FemaleGender Graduate SouthAsian [pweight=rakedweight], robust 
eststo
esttab

pwcorr NonTransphobia White Black EastAsian SouthAsian [aweight=rakedweight], sig
